package cn.itsource.pethome.user.service.impl;

import cn.itsource.pethome.base.service.impl.BaseServiceImpl;
import cn.itsource.pethome.constants.BaseConstants;
import cn.itsource.pethome.user.domain.Logininfo;
import cn.itsource.pethome.user.domain.User;
import cn.itsource.pethome.user.domain.dto.UserDTO;
import cn.itsource.pethome.user.mapper.LogininfoMapper;
import cn.itsource.pethome.user.mapper.UserMapper;
import cn.itsource.pethome.user.service.IUserService;
import cn.itsource.pethome.util.BusinessException;
import cn.itsource.pethome.util.MD5Utils;
import cn.itsource.pethome.util.StrUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zcj
 * @since 2022-10-13
 */
@Service
public class UserServiceImpl extends BaseServiceImpl<User> implements IUserService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private LogininfoMapper logininfoMapper;

    @Override
    public void register(UserDTO userDTO) {
        //1.验证手机号是否为null
        //2.手机验证码是否为null
        //3.密码是否为null
        //4.确认密码是否为null
        if(StringUtils.isEmpty(userDTO.getPhone())){
            throw new BusinessException("手机号不能为空");
        }
        if(StringUtils.isEmpty(userDTO.getCode())){
            throw new BusinessException("手机验证码不能为空");
        }
        if(StringUtils.isEmpty(userDTO.getPassword())){
            throw new BusinessException("密码不能为空");
        }
        if(StringUtils.isEmpty(userDTO.getPasswordRepeat())){
            throw new BusinessException("确认密码不能为空");
        }
        //5.密码和确认密码是否一致
        if(!userDTO.getPasswordRepeat().equals(userDTO.getPassword())){
            throw new BusinessException("两次密码不一致");
        }
        //6.手机号是否被注册过
        User user = userMapper.queryByPhone(userDTO.getPhone());
        if(user != null){
            throw new BusinessException("该手机号已经被注册");
        }
        //7.根据手机号+业务键 去redis中获取手机验证
        Object code = redisTemplate.opsForValue().get(BaseConstants.register.REGISTER + userDTO.getPhone());
        if(code != null){
            //7.1 有
            //7.1.1 判断两个验证码是否一致
            String[] split = code.toString().split(":");
            if(split[0].equalsIgnoreCase(userDTO.getCode())){
               //7.1.2 将数据保存到数据库
                User user1 = new User();
                user1.setPhone(userDTO.getPhone());
                user1.setUsername(userDTO.getPhone());
                user1.setPassword(userDTO.getPassword());
                user1.setState(BaseConstants.User.STATE_ACTIVATED);
                this.insert(user1);
            }else {
                throw new BusinessException("验证码输入错误，请重新输入");
            }
        }else{
            //7.2 无
            //7.2.1 直接抛出 验证已失效
            throw new BusinessException("验证码已失效,请重新获取");
        }
    }

    @Transactional
    @Override
    public void insert(User user) {//user  ox011
        //this.insert();
        this.encryption(user);//user  ox011
        // 7.1.3 将logininfo也保存一份
        Logininfo logininfo = new Logininfo();
        BeanUtils.copyProperties(user,logininfo);
        logininfo.setDisable(1);
        logininfo.setType(1);
        logininfoMapper.insert(logininfo);

        user.setLogininfoId(logininfo.getId());
        userMapper.insert(user);
    }

    /**
     * 给密码加密
     * @param user
     */
    private void encryption(User user){//user  ox011
        String RandomString = StrUtils.getComplexRandomString(32);
        user.setSalt(RandomString);//先不管
        String pwd = MD5Utils.encrypByMd5(user.getPassword() + RandomString);
        user.setPassword(pwd);
    }

    /**
     * 只是为了完成 user表和logininfo的同步
     * @param user
     */
    @Transactional
    @Override
    public void update(User user) {
        //调用user的修改
        super.update(user);
        //因为修改的时候可能没有LogininfoId
        User user1 = super.queryOne(user.getId());
        //从user表中去获取logininfo的id
        Long logininfoId = user1.getLogininfoId();
        //去查询logininfo 的数据
        Logininfo logininfo = logininfoMapper.queryOne(logininfoId);
        //获取之前的id
        Long id = logininfo.getId();
        //将修改后的user复制给logininfo
        BeanUtils.copyProperties(user1,logininfo);
        //将之前的id设置回来
        logininfo.setId(id);
        //修改logininfo
        logininfoMapper.update(logininfo);
    }

    @Transactional
    @Override
    public void delete(Long id) {
        User user1 = super.queryOne(id);
        logininfoMapper.delete(user1.getLogininfoId());
        super.delete(id);
    }
}
